package com.study_online.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study_online.domain.PageBean;
import com.study_online.domain.ResultInfo;
import com.study_online.domain.Video;
import com.study_online.mapper.SearchMapper;
import com.study_online.mapper.UserMapper;
import com.study_online.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 孙远帅
 */
@Service
public class SearchServiceImpI implements SearchService {

    @Autowired
    private UserMapper userMapper;
    @Resource
    private SearchMapper searchMapper;

    /**
     * 首页搜索
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @Override
    public ResultInfo searchKeywordsList(Integer pageNum, Integer pageSize, String keywords) {

        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        List list = new ArrayList();
        //启动分页显示插件
        PageHelper.startPage(pageNum, pageSize);

        try{
            //通过传入的参数根据情况获得相关视频信息
            List<Video> searchVideoList = searchMapper.searchVideoList(keywords);
            //设置分页插件
            PageInfo<Video> searchPageInfo = new PageInfo<>(searchVideoList);
            //将分页相关数据添加到Page中
            PageBean page=new PageBean(searchPageInfo.getPrePage(),searchPageInfo.getPageNum(),
                    searchPageInfo.getNextPage(),searchPageInfo.getPageSize(),searchPageInfo.getTotal(),
                    searchPageInfo.getPages(),searchPageInfo.isIsFirstPage(),searchPageInfo.isIsLastPage());

            //将视频信息以及分页信息设置到对象中
            list.add(searchVideoList);
            list.add(page);
            info.setData(list);
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("查找失败");

        }
        //返回数据
        return info;
    }
}
